package com.buaa.hyf.activity;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.buaa.hyf.util.Globals;
import com.buaa.hyf.util.PageDao;
import com.buaa.hyf.util.TxtDao;
import com.buaa.hyf.view.MyView;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {

    private Map<String,Object> txtMap;
    private Map<String,Object> pageMap = new HashMap<>();

    private StringBuffer sb;

    //行数
    private int lineCount;
    //总页数
    private int pagenum=1;
    //当前页数
    private int nowpage=1;

    private MyView contentView;
    private TextView pageView;

    private String fullPath;

    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Globals.init(this);
        setContentView(R.layout.activity_main);

        contentView = (MyView) findViewById(R.id.content_view);
        pageView = (TextView) findViewById(R.id.page_view);


        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0){
                    pageView.setText("正在分页，已经分解"+(pagenum-1)+"页");
                }else if(msg.what==1){
                    pageView.setText("分页结束"+nowpage+"/"+pagenum);
                }

            }
        };



        //得到小说的全路径
        fullPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/yangjiang.txt";

        //将全路径的的这些信息放到txt数据库里，因此需要建立这两张表的dao层
        TxtDao.insertTxtDao(fullPath);

        txtMap = TxtDao.findTxtByFullpath(fullPath);
        //如果文本是第一次读进来的，则分页，如果不是第一次读进来的，则跳转到上次读的地方
        if ((int)txtMap.get("overFlag")==0){
            //第一次读进来的，需要分页

            Thread thread = new Thread(){
                @Override
                public void run() {
                    try {
                        BufferedReader br = new BufferedReader(new InputStreamReader
                                (new FileInputStream(fullPath),"GBK"));
                        String line;
                        while ((line=br.readLine())!=null){
                            while(line.length()>Globals.LINT_CHAR_COUNT){  //如果数据库里记录的一行数据字数大于规定显示在手机屏幕里的一行的字数
                                String str = line.substring(0,Globals.LINT_CHAR_COUNT);
                                line=line.substring(Globals.LINT_CHAR_COUNT);
                                addline(str);

                            }
                            addline(line);
                        }

                        br.close();
                        handler.sendEmptyMessage(1);
                        TxtDao.updateTxtOverFlag(fullPath);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };

            thread.start();

        }else{
            //需要跳转到上次读的位置

        }
    }

    public void addline(String str){
        sb.append(str);
        sb.append(Globals.END_FLAG);
        lineCount++;
        if (lineCount==Globals.LINE_COUNT){    //如果截取到的字符够一页纸的时候
            pageMap.put("txtid",txtMap.get("txtid"));
            pageMap.put("pagenum",pagenum++);
            pageMap.put("content",sb.toString());
            PageDao.InsertPageData(pageMap);

            handler.sendEmptyMessage(0);
            sb = new StringBuffer();
            lineCount=0;
        }
    }

    public Map<String, Object> getTxtMap() {
        return txtMap;
    }

    public void setTxtMap(Map<String, Object> txtMap) {
        this.txtMap = txtMap;
    }

    public int getPagenum() {
        return pagenum;
    }

    public void setPagenum(int pagenum) {
        this.pagenum = pagenum;
    }

    public int getNowpage() {
        return nowpage;
    }

    public void setNowpage(int nowpage) {
        this.nowpage = nowpage;
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }
}
